package com.framework.bdf4j.socket.sclient;



	import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
//https://blog.csdn.net/baidu_34219514/article/details/110440899
	public class SocketClient {

	    public static void main(String[] args) throws IOException {

	        String ip = "127.0.0.1";
	        int port = 8088;
	        String charSetType = "UTF-8";
			String reqMsg="<service><prcscd>pfms03</prcscd>\r\n" +
					"<servtp>HZF</servtp>\r\n" + 
					"<servsq>20200608000000000001</servsq>\r\n" + 
					"<userid>0170901</userid>\r\n" + 
					"<brchno>017001</brchno>\r\n" + 
					"<job_number>GH88888888</job_number>\r\n" +
					"<name>测试小陈</name>\r\n" +
					"<sex>男</sex>\r\n" +
	                "<birthday>134</birthday>\r\n" +
	                "<organ_dept_name>2341341</organ_dept_name>\r\n" +
	                "<nation>1</nation>\r\n" +
	                "<is_store>1</is_store>\r\n" +
	                "<id_card>1</id_card>\r\n" +
	                "<political_status>1</political_status>\r\n" +
	                "<name_once_used>1</name_once_used>\r\n" +
	                "<file_number>1</file_number>\r\n" +
	                "<birth_place>1</birth_place>\r\n" +
	                "<native_place>1</native_place>\r\n" +
	                "<in_work_day>2020-03-11</in_work_day>\r\n" +
	                "<is_league_member>1</is_league_member>\r\n" +
	                "<in_member_day>2020-03-11</in_member_day>\r\n" +
	                "<in_party_day>2020-03-11</in_party_day>\r\n" +
	                "<health_condition>1</health_condition>\r\n" +
	                "<full_education_degree>1</full_education_degree>\r\n" +
	                "<full_colleges_majors>1</full_colleges_majors>\r\n" +
	                "<part_education_degree>1</part_education_degree>\r\n" +
	                "<part_colleges_majors>1</part_colleges_majors>\r\n" +
	                "<now_post>1</now_post>\r\n" +
	                "<establishment_organization>1</establishment_organization>\r\n" +
	                "<secondary_organization>21</secondary_organization>\r\n" +
	                "<station>1</station>\r\n" +
	                "<post>1</post>\r\n" +
	                "<line>1</line>\r\n" +
	                "<nature_employment>1</nature_employment>\r\n" +
	                "<rank>1</rank>\r\n" +
	                "<person_state>1</person_state>\r\n" +
	                "<is_manager>1</is_manager>\r\n" +

	                "<jobresume>\n" +
	                "<record>\n" +
	                "<begin_time>2020-03-11</begin_time>\r\n" +
	                "<end_time>2020-03-11</end_time>\r\n" +
	                "<post>职务</post>\r\n" +
	                "<operate>edit</operate>\r\n" +
	                "<company>112</company>\r\n" +
	                "</record>\n" +
	                "<record>\n" +
	                "<begin_time>2020-03-11</begin_time>\r\n" +
	                "<end_time>2020-03-11</end_time>\r\n" +
	                "<post>职务</post>\r\n" +
	                "<operate>edit</operate>\r\n" +
	                "<company>112</company>\r\n" +
	                "</record>\n" +

	                "</jobresume>\r\n"+

	                "<education>\n" +
	                "<record>\n" +
	                "<in_school_day>2020-03-11</in_school_day>\r\n" +
	                "<school_name>2020-03-11</school_name>\r\n" +
	                "<major_name>2020-03-11</major_name>\r\n" +
	                "<education_level>本科</education_level>\r\n" +
	                "</record>\n" +
	                "<record>\n" +
	                "<in_school_day>2020-03-11</in_school_day>\r\n" +
	                "<school_name>2020-03-11</school_name>\r\n" +
	                "<major_name>2020-03-11</major_name>\r\n" +
	                "<education_level>本科</education_level>\r\n" +
	                "</record>\n" +
	                "</education>\r\n"+

					"</service>";
			//reqMsg="<service><prcscd>pfms03</prcscd>\r\n" ;
	        OutputStream out = null; // 写
	        InputStream in = null; // 读
	        String respData = ""; // 响应报文
	        Socket socket = new Socket(); // 客户机
	        ByteArrayOutputStream bytesOut = null;
	        try {
	            socket.setTcpNoDelay(true);//关闭socket的缓冲,将数据立即发送出去
	            socket.setReuseAddress(true);
	            socket.setSoLinger(true, 0);
	            socket.setSendBufferSize(32*1024);//发送缓存区
	            socket.setReceiveBufferSize(32*1024);//接收缓存区
	            socket.setKeepAlive(true);//短连接
	            socket.connect(new InetSocketAddress(ip, port), 5000);
	            /**
	             * 发送TCP请求
	             */
	            out = socket.getOutputStream();
	            String requestData = setLength(reqMsg, charSetType);
	           //System.out.println("请求数据：" + requestData);
	            out.write(requestData.getBytes(charSetType));
	            out.flush();

	            /**
	             * 接收TCP响应
	             */
	            socket.setSoTimeout(60000);//socket调用InputStream读数据的超时时间，以毫秒为单位
	            in = socket.getInputStream();
	            //1、读取数据长度
	            byte[] lenBytes = new byte[8];
	            in.read(lenBytes,0,8);
	            
	            String s =  new String(lenBytes,"UTF-8");
	            Integer length = Integer.valueOf(new String(lenBytes));
	            //System.out.println(s+"返回数据长度：" + length);

	            //2、读取报文数据
	            byte[] data = new byte[1024 * 4];
	            int len = 0;
	            bytesOut = new ByteArrayOutputStream();
	            Integer dataLen = 0;
	            while ((len = in.read(data)) != -1) {
	                bytesOut.write(data, 0, len);
	                dataLen = bytesOut.toByteArray().length;

	                if(length.equals(dataLen)){
	                    break;
	                }
	            }
	            System.out.println("返回数据----：" + bytesOut.toString(charSetType));

	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (out != null) {
	                    out.close();
	                }
	                if (socket != null) {
	                    socket.close();
	                }
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }

	    private static String setLength(String str, String charSetType) throws UnsupportedEncodingException {
	        // 完整报文：10位长度 + 报文内容
//	        return String.format("%010d", str.getBytes(charSetType).length) + str;
	        return String.format("%08d", str.getBytes(charSetType).length) + str;

	    }

	}

